package com.onemore.task.remote;

import java.sql.SQLException;
import java.util.Date;

import org.voovan.http.server.HttpResponse;
import org.voovan.tools.log.Logger;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.onemore.task.OneJdbc;
import com.onemore.task.model.ModelCangku;
import com.onemore.task.model.ModelJorder;
import com.onemore.task.model.ModelJordermx;
import com.onemore.task.model.ModelJsend;
import com.onemore.task.model.ModelKuWei;
import com.onemore.task.model.ModelShangpin;
import com.onemore.task.model.ModelSpgg1;
import com.onemore.task.model.ModelSpgg2;
import com.onemore.task.model.ModelVendor;

public class TaskJorder extends RmtTaskBase{
	String required_field[] = {"DJBH","RQ","QYDM",////结算渠道
			"DM1",//供货商代码*
			"QDDM",////渠道代码
			"BYZD1",//价格选定
			"BYZD12",//折扣
		"YGDM",//业务员
		"DM4"//订单类型
		,"YXRQ"//交货日
	};
	public void process(JSONObject jsonObject,HttpResponse response)
	{
		JSONObject jorderJson = jsonObject;
		//规格有效性校验
		int ret = 0;
		outer:do {
			JSONArray itemmxArray = jorderJson.getJSONArray("ITEM_JORDERMX");
			if(itemmxArray == null) {
				message = "接收到的采购订单报文，未填写ITEM_JSENDMX项";
				Logger.error(message);
				ret = -100;
				break outer;
			}
			////////////////////////////////////////判断单据编号
			if(!jorderJson.containsKey("DJBH") || jorderJson.getString("DJBH") == "") {
				message = "采购通知单单据编号(DJBH)不能为空";
				Logger.error(message);
				ret = -528;
				break outer;
	    	}
	    	try {
				if(ModelJorder.isExist(jorderJson.getString("DJBH"))) {//BYZD6 SCM的明细单号
					message = "采购通知单单据编号(DJBH)已存在,DJBH:"+jorderJson.getString("DJBH");
					Logger.error(message);
					ret = -529;
					break outer;
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				message = e.getMessage();
				Logger.error(message);
				ret = -527;
				break outer;
			}
	    	////////////////////////////////////
			String djbh = jorderJson.getString(ModelJsend.jsend_field[0]);
			for(int j=0;j<itemmxArray.size();j++) {
				JSONObject itemObject = itemmxArray.getJSONObject(j);
				String mxdjbh = itemObject.getString(ModelJsend.jsend_field[0]);
				if(!mxdjbh.startsWith(djbh)) {
					message = "明细表单据编号跟主表单据编号不统一";
					Logger.error(message);
					ret = -500;
					break outer;
				}
				try {
					if(!ModelShangpin.isValid(itemObject.getString("SPDM"))) {
						message = "SCM与ERP商品代码未同步,在ERP数据库找不到对应的商品代码或该商品信息未启用";
						Logger.error(message);
						ret = -533;
						break outer;
					}
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					Logger.error(message);
					ret = -532;
					break outer;
				} 

	    		try {
					setDefault(itemObject,"CKJ",ModelShangpin.getBZJJ(itemObject.getString("SPDM")));
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					message = e1.getMessage();
					Logger.error(message);
					ret = -532;
					break outer;
				}
		    	/*if(!itemObject.containsKey("BYZD6") || itemObject.getString("BYZD6") == "") {
					message = "明细表单编号(BYZD6)不能为空";
					Logger.error(message);
					ret = -521;
					break outer;
		    	} */
		    	/*try {
					if(OneJdbc.search("JSENDMX", "BYZD6='"+itemObject.getString("BYZD6")+"'")) {//BYZD6 SCM的明细单号
						message = "明细表单据编号已存在";
						Logger.error(message);
						ret = -522;
						break outer;
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					message = e.getMessage();
					Logger.error(message);
					ret = -523;
					break outer;
				}*/
			}

	    	setDefault(jorderJson,"DM1","000");//供货商代码 参照GongHuoShang表的GHSDM（成品供货商，成品/物料供货商）
	    	setDefault(jorderJson,"QDDM","000");//渠道代码
	    	setDefault(jorderJson,"DM4","000");//订单类型
//	    	setDefault(jorderJson,"DM2","000");//仓库代码  参照CANGKU表CKDM
	    	setDefault(jorderJson,"DM2_1","000");//库位代码 参照KUWEI表KWDM/且符合CKKW表
	    	setDefault(jorderJson,"QYDM","000");//结算渠道代码 ???
	    	setDefault(jorderJson,"YGDM","000");//业务员
	    	setDefault(jorderJson,"BYZD12","1");//折扣
	    	setDefault(jorderJson,"BYZD1","0");//价格选定
	    	setDefault(jorderJson,"BYZD2","0");//单据状态 未锁定YXRQ
	    	setDefault(jorderJson,"BYZD2","0");//单据状态 未锁定
	    	setDefault(jorderJson,"LLR","0");//打印
	    	setDefault(jorderJson,"SP","0");
	    	setDefault(jorderJson,"JS","0");
	    	setDefault(jorderJson,"XC","0");
	    	setDefault(jorderJson,"TJ","0");
	    	setDefault(jorderJson,"YS","1");//审核1
	    	setDefault(jorderJson,"YSRQ",df.format(new Date()));//审核1日期
	    	setDefault(jorderJson,"JZ","1");//审核2
	    	setDefault(jorderJson,"JZRQ",df.format(new Date()));//审核2日期
	    	try {
		    	checkOutRequired(jorderJson,required_field);
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				message = e.getMessage();
				ret = -533;
				Logger.error(e);
				break outer;
			}
	    	try {
				ModelVendor.checkVendorValid(jorderJson.getString("DM1"));
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				message = e1.getMessage();
				ret = -534	;
				Logger.error(e1);
				break outer;
			}
			for(int i=1;i<11;i++) {
				try {
					if(OneJdbc.insert_jorder(jorderJson) ) {
						break;
					}else {
						message = "插入采购通知失败,错误计数："+i;
						Logger.error(message);
						if(i == 1) {
							ret = -1;
							break;	    								
						}
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					message = e.getMessage();
					ret = -218;
					Logger.error(e);
					break outer;
				}
			}
			for(int j=0;j<itemmxArray.size();j++) {
				JSONObject jordermx = itemmxArray.getJSONObject(j);
    			for(int i=1;i<11;i++) {
    				try {
						if(OneJdbc.insert_jordermx(jordermx)) {
							break;
						}else {
							message = "插入采购通知明细表失败,错误计数："+i;
							Logger.error(message);
							if(i == 1) {
								ret = -1;
								break;	    								
							}
						}
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						message = e.getMessage();
						ret = -219;
						Logger.error(e);
						break outer;
					}
    			}
			}
		}while(false);
		if(ret == 0 || ret == 200) {
			Logger.info("成功插入从SCM获取采购订单的数据，ret:"+ret+" message:"+message);
		}
        putResult(response, ret, message);
	}
}
